<!-- 首页，支持店铺装修 -->
<template>
  <view>
    <view v-if="!template" class="loading">加载中...</view>
    <view v-else>
      <view class="debug-info">
        <text>组件数量: {{ template.components ? template.components.length : 0 }}</text>
      </view>      
      <s-layout
        title="首页"
        navbar="custom"
        tabbar="/pages/index/index"
        :bgStyle="template.page"
        :navbarStyle="template.navigationBar"
        onShareAppMessage
      >
        <s-block
          v-for="(item, index) in template.components"
          :key="index"
          :styles="item.property.style"
        >
          <s-block-item :type="item.id" :data="item.property" :styles="item.property.style" />
        </s-block>
      </s-layout>
    </view>
  </view>
</template>

<script setup>
  import { computed } from 'vue';
  import { onLoad, onPageScroll, onPullDownRefresh } from '@dcloudio/uni-app';
  import sheep from '@/sheep';
  import $share from '@/sheep/platform/share';
  // 隐藏原生tabBar
  uni.hideTabBar({
    fail: () => {},
  });

  const template = computed(() => {
    const appTemplate = sheep.$store('app').template;
    console.log('首页模板数据:', appTemplate);
    // 适配新的模板数据结构
    const homeData = appTemplate?.home || appTemplate?.basic?.home;
    console.log('首页组件数据:', homeData);
    return homeData;
  });
  

  onLoad((options) => {
    // #ifdef MP
    // 小程序识别二维码
    if (options.scene) {
      const sceneParams = decodeURIComponent(options.scene).split('=');
      console.log('sceneParams=>', sceneParams);
      options[sceneParams[0]] = sceneParams[1];
    }
    // #endif

    // 预览模板
    if (options.templateId) {
      sheep.$store('app').init(options.templateId);
    }

    // 解析分享信息
    if (options.spm) {
      $share.decryptSpm(options.spm);
    }

    // 进入指定页面(完整页面路径)
    if (options.page) {
      sheep.$router.go(decodeURIComponent(options.page));
    }
  });

  // 下拉刷新
  onPullDownRefresh(() => {
    sheep.$store('app').init();
    setTimeout(function () {
      uni.stopPullDownRefresh();
    }, 800);
  });

  onPageScroll(() => {});
</script>

<style>
  .loading {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    color: #666;
  }

  .debug-info {
    padding: 10px;
    background-color: #f5f5f5;
    margin-bottom: 10px;
    font-size: 14px;
    color: #333;
  }
</style>
